﻿/*
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
using System;
using LeftronicApi.Models.Factories.Single;

namespace LeftronicApi.Models.Wire.Single
{
    public class Builder
    {
        public static BaseModel Build(string accessKey, Models.BaseModel point)
        {
            if (point as Models.NumberPoint != null)
            {
                var nbr = point as Models.NumberPoint;
                return !nbr.Number.HasValue
                    ? (BaseModel) ClearFactory.Create(accessKey, nbr.StreamName)
                    : NumberFactory.Create(accessKey, nbr);
            }

            if (point as Models.Label != null)
            {
                return LabelFactory.Create(accessKey, (Models.Label) point);
            }

            if (point as Models.BarChart != null)
            {
                var chart = point as Models.BarChart;
                return chart.Points.Count == 0
                    ? (BaseModel) ClearFactory.Create(accessKey, chart.StreamName)
                    : BarChartFactory.Create(accessKey, chart);
            }

            if (point as Models.Image != null)
            {
                return ImageFactory.Create(accessKey, (Models.Image) point);
            }

            if (point as Models.TextFeed != null)
            {
                var textFeed = point as Models.TextFeed;
                return textFeed.Feed.Count == 0
                    ? (BaseModel) ClearFactory.Create(accessKey, textFeed.StreamName)
                    : TextFeedFactory.Create(accessKey, textFeed);
            }

            if (point as Models.Html != null)
            {
                return HtmlFactory.Create(accessKey, (Models.Html) point);
            }

            throw new InvalidOperationException("Invalid point passed to builder");
        }
    }
}
